package xhr03;

import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.exception.RemotingException;

import java.nio.charset.StandardCharsets;

public class Producer2 {
    public static void main(String[] args) throws Exception {
        // 1.创建生产者对象
        DefaultMQProducer defaultMQProducer = new DefaultMQProducer("producer_pethome_group");
        // 2.设置NameServer地址,均衡调用对象
        defaultMQProducer.setNamesrvAddr("127.0.0.1:9876");
        // 3.启动producer实例
        defaultMQProducer.start();
        // 4.创建消息
        Message message = new Message("pethome_topic", "pethome_tags", "在家写的一个异步请求发送".getBytes(StandardCharsets.UTF_8));
        // 5.发送异步消息消息
        defaultMQProducer.send(message, new SendCallback() {
            // 消息发送成功
            @Override
            public void onSuccess(SendResult sendResult) {
                System.out.println(sendResult);
            }
            // 消息发送失败
            @Override
            public void onException(Throwable throwable) {
                throwable.printStackTrace();
                System.out.println("消息发送失败!");
            }
        });
    }
}
